<template>
  <div>
    <el-table
      v-loading="loading"
      :data="tableList"
      highlight-current-row
      :span-method="objectSpanMethod"
    >
      <el-table-column label="科室名称" align="center" prop="deptName">
        <template slot-scope="scope">
          <el-row>
            <el-col :span="12">
              <span>{{ scope.row.deptName }}({{ scope.row.userName }}) </span>
            </el-col>
            <el-col :span="12">
              <el-button
                type="text"
                size="small"
                @click="handleReject(scope.row)"
              >
                退回
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="handleAllResult(scope.row)"
              >
                历次结果
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="handleExpand(scope.row)"
              >
                {{ !scope.row.showFlag ? "展开" : "收起" }}
              </el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="检查项目" align="center" prop="projectName">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="!scope.row.showFlag"
            @click="handleExpand(scope.row)"
            >查看详情</el-button
          >
          <span v-else>{{ scope.row.projectName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检查结果" align="center" prop="result" />
      <el-table-column label="范围" align="center" prop="range" />
    </el-table>

    <!-- 对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="复查项目" prop="projectId">
          <el-select
            v-model="form.projectId"
            filterable
            clearable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in list"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="复查时间" prop="reviewTime">
          <el-date-picker
            v-model="form.reviewTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "TableData",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [
        {
          id: "111111",
          deptName: "体格检查",
          deptId: 1,
          children: [
            {
              projectName: "舒张压",
              projectId: "1",
              result: "aaaa",
              range: "12",
            },
            {
              projectName: "收缩压",
              projectId: "2",
              result: "aaaa",
              range: "12",
            },
            {
              projectName: "心率",
              projectId: "3",
              result: "aaaa",
              range: "12",
            },
          ],
          userName: "admin",
          projectName: "舒张压",
          isOpen: false,
        },
        {
          id: "222222",
          deptName: "心电图",
          deptId: 2,
          userName: "admin",
          children: [],
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 当前页
      activeName: "3",
      //表格展示列表
      tableList: [],
    };
  },
  computed: {},
  created() {
    this.getTableList(this.list);
  },
  methods: {
    // 历次结果
    handleAllResult() {
      this.reset();
      this.open = true;
      this.title = "历次结果";
    },
    // 退回
    handleReject(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否要把本项目退回给分检医生?")
        .then(function () {
          // return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("退回成功");
        })
        .catch(() => {});
    },
    // 重置表格数据
    getTableList(list) {
      this.tableList = [];
      for (let ele of list) {
        let newArray = [];
        if (!!ele.showFlag && ele.children && ele.children.length > 0) {
          const length = ele.children.length;
          for (let e of ele.children) {
            newArray.push({
              ...e,
              id: ele.id,
              deptId: ele.deptId,
              deptName: ele.deptName,
              userName: ele.userName,
              length: length,
              firstIndex: this.tableList.length,
              showFlag: !!ele.showFlag,
            });
          }
        } else {
          newArray.push({
            id: ele.id,
            deptId: ele.deptId,
            deptName: ele.deptName,
            userName: ele.userName,
            length: 1,
            firstIndex: this.tableList.length,
            showFlag: !!ele.showFlag,
          });
        }
        this.tableList = this.tableList.concat(newArray);
      }
      // console.log(this.tableList);
    },
    // 展开
    handleExpand(row) {
      const showFlag = !row.showFlag;
      let element = this.list.find((e) => row.deptId == e.deptId);
      element.showFlag = !element.showFlag;
      this.getTableList(this.list);
    },
    // 合并表格
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(row, column, rowIndex, columnIndex);
      if (columnIndex === 0) {
        const firstIndex = row.firstIndex;
        if (rowIndex == firstIndex) {
          const length = row.length;
          return {
            rowspan: length,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // listProject(this.queryParams).then((response) => {
      //   this.projectList = response.rows
      //   this.total = response.total
      //   this.loading = false
      // })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        categoryId: null,
        projectName: null,
      };
      this.resetForm("form");
    },
  },
};
</script>

<style>
</style>
